package com.jic.product.impl;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.product.constant.consist.ResponseCode;
import com.jic.product.entity.ProductBom;
import com.jic.product.exception.AppRuntimeException;
import com.jic.product.manage.request.ProductBomBatchRequest;
import com.jic.product.manage.request.ProductBomRequest;
import com.jic.product.manage.response.ProductBomResponse;
import com.jic.product.mapper.ProductBomMapper;
import com.jic.product.service.ProductBomService;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 商品bom表
 *
 * @author :
 * @version : v1.0.0
 * @email :
 * @since : 2020-04-23 11:00:18
 */
@Service
@Slf4j
public class ProductBomServiceImpl implements ProductBomService {

    @Autowired
    private ProductBomMapper productBomMapper;

    @Override
    public RestResult<String> insert(ProductBomBatchRequest request) {
        try {
            ProductBom productBom = new ProductBom();
            BeanUtils.copyProperties(request, productBom);
            for (int i = 0; i < request.getProductIds().size(); i++) {
                productBom.setProductId(request.getProductIds().get(i));
                Integer queryCount = productBomMapper.queryCount(productBom);
                if (queryCount > 0) {
                    continue;
                }
                productBomMapper.insert(productBom);
            }
        } catch (Exception e) {
            log.error("保存bom商品异常", e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION);
        }
        return RestResult.success("保存bom商品成功");
    }

    @Override
    public RestResult<String> delete(ProductBom productBom) {
        try {
            productBomMapper.delete(productBom);
        } catch (Exception e) {
            log.error("删除bom商品异常", e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION);
        }
        return RestResult.success("删除bom商品成功");
    }

    @Override
    public RestResult<String> update(ProductBom productBom) {
        try {
            productBomMapper.update(productBom);
        } catch (Exception e) {
            log.error("修改bom商品异常", e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION);
        }
        return RestResult.success("修改bom商品成功");
    }

    @Override
    public RestResult<List<ProductBomResponse>> query(ProductBomRequest productBomRequest) {
        ProductBom productBom = new ProductBom();
        BeanUtils.copyProperties(productBomRequest, productBom);
        List<ProductBomResponse> query = productBomMapper.query(productBom);
        return RestResult.success(query);
    }

    @Override
    public PageResult<ProductBom> queryPage(ProductBom productBom, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<ProductBom> list = productBomMapper
                .queryPage(startIndex, pageQuery.getLimit(), productBom);
        long count = productBomMapper.queryPageCount(productBom);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }



}